package com.xiaoyang.forum.controller;

import cn.hutool.core.util.StrUtil;
import com.xiaoyang.forum.common.code.*;
import com.xiaoyang.forum.common.util.JWTUtil;
import com.xiaoyang.forum.domain.dto.ArticleDTO;
import com.xiaoyang.forum.domain.dto.ArticleListDTO;
import com.xiaoyang.forum.domain.dto.ArticleModifyDTO;
import com.xiaoyang.forum.domain.vo.*;
import com.xiaoyang.forum.service.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: XiaoYang
 * @CreateTime: 2025-08-14
 * @Description: 帖子操作类
 */
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController {

    @Autowired
    private IArticleService articleService;

    /**
     * 创建文章
     * @param articleDTO
     * @return
     */
    @PostMapping("/create")
    public Result<ArticleUserNameVO> create(@RequestHeader String token,
                                            @RequestBody(required = false) ArticleDTO articleDTO) {
        // 1. 校验用户是否登录
        // 判断token是否为空
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN);
        }
        // 2. 校验版块id, 标题, 内容不能为空
        if (articleDTO == null
                || StrUtil.isEmpty(articleDTO.getTitle())
                || StrUtil.isEmpty(articleDTO.getContent())) {
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        Integer userId = JWTUtil.getUserIdFromToken(token);

        return Result.success(articleService.create(userId, articleDTO));
    }

    /**
     * 获取文章列表
     * @param ArticleListDTO
     * @return
     */
    @GetMapping("/semiLogin/toList")
    public TableDataInfo toList(ArticleListDTO ArticleListDTO) {
        log.info("获取文章列表");
        List<ArticleListVO> list;
        if (ArticleListDTO.getBoardId() != null) {
            // 查询版块文章列表
            list = articleService.selectById(ArticleListDTO);
        } else {
            // 查询全部文章列表
            list = articleService.selectAll(ArticleListDTO);
        }
        return getDataTable(list);
    }

    /**
     * 获取文章详情
     * @param id
     * @return
     */
    @GetMapping("/semiLogin/getDetails")
    public Result<ArticleUserVO> getDetails(@RequestHeader String token,
                                            Long id) {
        log.info("获取文章详情" + id);
        if (id == null) {
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        ArticleUserVO articleUserVO = articleService.getDetails(id);
        if (StrUtil.isEmpty(token)) {
            return Result.success(articleUserVO);
        } else {
            Integer userId = JWTUtil.getUserIdFromToken(token);
            if (userId == null) {
                // token校验失败
                return Result.failed(ResultCode.TOKEN_ERROR_CODE);
            }
            if (articleUserVO.getUserId().equals((long) userId)) {
                articleUserVO.setIsAuthor(true);
            }
            return Result.success(articleUserVO);
        }
    }

    /**
     * 修改文章
     * @param token
     * @param articleModifyDTO
     * @return
     */
    @PutMapping("/modify")
    public Result<ArticleModifyVO> modify(@RequestHeader String token,
                                          @RequestBody ArticleModifyDTO articleModifyDTO) {
        log.info("修改文章");
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN);
        }
        long userId = (long) JWTUtil.getUserIdFromToken(token);
        return Result.success(articleService.modify(userId, articleModifyDTO));
    }

    /**
     * 删除帖子
     * @param token
     * @param articleId
     * @return
     */
    @DeleteMapping("/delete")
    public Result<ArticleVO> delete(@RequestHeader String token, Long articleId) {
        log.info("删除文章");
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN);
        }
        long userId = (long) JWTUtil.getUserIdFromToken(token);
        return Result.success(articleService.deleteById(articleId, userId));
    }

    /**
     * 点赞
     * @param token
     * @param articleId
     * @return
     */
    @PutMapping("/thumbsUp")
    public Result<ArticleLikeVO> thumbsUp(@RequestHeader String token, Long articleId) {
        log.info("点赞");
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN);
        }
        long userId = (long) JWTUtil.getUserIdFromToken(token); // 用户id

        return Result.success(articleService.thumbsUp(userId, articleId));
    }

    /**
     * 获取用户点赞列表
     * @param userId 用户id
     * @return
     */
    @GetMapping("/getUserThumbsUpList")
    public Result<List<ArticleUserLikeListVO>> getUserThumbsUpList(Long userId) {
        log.info("{}的点赞列表", userId);
        return Result.success(articleService.getUserThumbsUpList(userId));
    }

    /**
     * TA的精选
     * @param userId
     * @return
     */
    @GetMapping("/semiLogin/getUserChoiceness")
    public Result<List<ArticleRelatedVO>> getUserChoiceness(Long userId) {
        log.info("{}的精选", userId);
        return Result.success(articleService.getUserChoiceness(userId));
    }
}
